<?php

namespace App\Models;

use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use Dcat\Admin\Grid\Model;
use Dcat\Admin\Repositories\Repository;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

class Domain extends Repository
{
    protected string $keyName = 'DomainName';

    /**
     * @throws ClientException
     */
    public static function initAliyun(Aliyun $aliyun): string
    {
        $aliyun->initClient();

        return self::class;
    }

    /**
     * @throws ClientException
     * @throws ServerException
     */
    public function get(Model $model): array|LengthAwarePaginator|Collection
    {
        $currentPage = $model->getCurrentPage();
        $perPage = $model->getPerPage();

        $res = \AlibabaCloud\Domain\Domain::v20180129()
            ->queryDomainList()
            ->withPageSize($perPage)
            ->withPageNum($currentPage)
            ->request();

        return $model->makePaginator(
            $res->get('TotalItemNum'),
            $res->get('Data.Domain')
        );
    }
}
